<?php

namespace App\Http\Controllers;


use App\Models\Post;
use App\Http\Requests\StorePostRequest;
use App\Http\Requests\UpdatePostRequest;


use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;//用这个然后下面的控制器的类继承BaseController的类来解决Method 'middleware' not found in FansController(这里是base还是其他的名字看情况而定)
use Illuminate\Support\Facades\Auth;
use Illuminate\Foundation\Validation\ValidatesRequests; // 导入这个 trait来解决:Method App\Http\Controllers\PostController::validate does not exist.
use Illuminate\Foundation\Auth\Access\AuthorizesRequests; // 导入 AuthorizesRequests trait来解决Method App\Http\Controllers\PostController::authorize does not exist.

// 确保导入这个类

class PostController extends BaseController
{

    use ValidatesRequests, AuthorizesRequests; // 使用 AuthorizesRequests 和 ValidatesRequests trait  解决authorize

    //下面所有方法都得是登录状态才能进入
    public function __construct()
    {
        $this->middleware('auth');
    }

    //文章列表
    public function index()
    {
        //
    }

    //创建文章的视图界面
    public function create()
    {
        //
    }

    //创建文章的业务逻辑
    public function store(Request $request)
    {
        $this->validate($request,[
            'content' => ['required','max:255']
            ]);
        Auth::user()->posts()->create([
           'content'=>$request['content']
        ]);
        session()->flash('success','文章发表成功');
        return redirect()->back();
    }

    //显示某一文章的信息
    public function show(Post $post)
    {
        //
    }

    //修改文章的视图界面
    public function edit(Post $post)
    {
        //
    }

    //修改文章的业务逻辑
    public function update(UpdatePostRequest $request, Post $post)
    {
        //
    }

    //删除一篇文章的
    public function destroy(Post $post)
    {
        $this->authorize('destroy',$post);
        $post->delete();
        session()->flash('success','文章删除成功');
        return redirect()->back();
    }
}
